package com.fwj.authorization.staff.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fwj.authorization.staff.entity.SysPermission;
import com.fwj.authorization.staff.entity.SysRole;
import com.fwj.authorization.staff.entity.SysUser;
import com.fwj.authorization.staff.service.SysUserService;
import com.fwj.base.entity.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户控制器
 *
 * @author 樊温军
 * @date 2025/3/18 上午10:32
 */
@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "用户控制器")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    /**
     * 用户列表——分页
     *
     * @return R<IPage < SysRole>>
     */
    @GetMapping("/page")
    @ApiOperation(value = "用户列表——分页")
    public R<IPage<SysUser>> getPage(Page<SysUser> page, SysUser sysUser) {
        try {
            IPage<SysUser> iPage = sysUserService.getPage(page, sysUser);
            return R.ok(iPage, "查询成功！");
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.failed(e.getMessage());
        }
    }

    /**
     * 查询某用户的所有权限
     *
     * @return R<List < SysPermission>>
     */
    @GetMapping("/permissionOfUser")
    @ApiOperation(value = "查询某用户的所有权限")
    public R<List<SysPermission>> permissionOfUser(@RequestParam("username") String username) {
        try {
            List<SysPermission> list = sysUserService.getPermissionListByUsername(username);
            return R.ok(list);
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.failed(e.getMessage());
        }
    }

    /**
     * 查询某用户的所有角色
     *
     * @return R<List < SysRole>>
     */
    @GetMapping("/roleOfUser")
    @ApiOperation(value = "查询某用户的所有角色")
    public R<List<SysRole>> roleOfUser(@RequestParam("username") String username) {
        try {
            List<SysRole> list = sysUserService.getRoleListByUsername(username);
            return R.ok(list);
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.failed(e.getMessage());
        }
    }


}

